package com.mz.mattress.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.mattress.admin.api.req.OrderUseRecordBonusQueryReq;
import com.mz.mattress.admin.api.resp.BonusMonthSum;
import com.mz.mattress.admin.api.resp.BonusDaySum;
import com.mz.mattress.admin.mapper.OrderUseTimesBonusMapper;
import com.mz.mattress.admin.service.OrderUseTimesBonusService;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.OrderUseTimesBonus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderUseTimesBonusServiceImpl implements OrderUseTimesBonusService {

    @Autowired
    private OrderUseTimesBonusMapper orderUseTimesBonusMapper;

    @Override
    public Result<BonusMonthSum> listMonths(Long memberId, Integer type, Integer typeId, String months) {

        List<BonusDaySum> list = orderUseTimesBonusMapper.listMonths(memberId, months, type, typeId);

        Integer total = 0;
        for (BonusDaySum bonusSum : list) {
            total += bonusSum.getSumBonus();
        }

        BonusMonthSum monthSum = new BonusMonthSum();
        monthSum.setMonths(months);
        monthSum.setTotalSum(total);
        monthSum.setList(list);

        return Result.ok(monthSum);
    }

    @Override
    public Result<IPage<OrderUseTimesBonus>> listPage(OrderUseRecordBonusQueryReq req) {

        IPage<OrderUseTimesBonus> listPage = orderUseTimesBonusMapper.listPage(req.newPage(),
                req.getMonths(), req.getDates(), req.getMemberId(), req.getType(), req.getTypeId());

        if (!listPage.getRecords().isEmpty()) {
            Integer sum = orderUseTimesBonusMapper.listPageSum(req.getMonths(), req.getDates(), req.getMemberId(), req.getType(), req.getTypeId());
            listPage.getRecords().get(0).setSumBonus(sum);
        }

        return Result.ok(listPage);
    }

    @Override
    public Result<OrderUseTimesBonus> getById(Integer id) {
        OrderUseTimesBonus record = orderUseTimesBonusMapper.selectById(id);
        return Result.ok(record);
    }

}
